import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import pages.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyBatisTest {

    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            User user = openSession.selectOne(
                    "mapper.UserMapper.findUserById", "1");

            UserMapper mapper = openSession.getMapper(UserMapper.class);

            List<User> list = new ArrayList<>();

            Long startTime = System.currentTimeMillis();
            for (int i=1000; i<2000; i++) {
//                list.add(new User((13512341324L + i) +"", "张三"+i, "更新1000~2000_" + i));
                mapper.updateOne(new User((13512341324L + i) +"", "张三"+i, "更新1000~2000_" + i));
            }
//            mapper.insertBatch(list); // 0~1000
//            mapper.updateOneByOne(list); // 1000 ~ 2000

            openSession.commit();
            System.out.println("使用了 " + (System.currentTimeMillis() - startTime) + "秒");


            System.out.println(user);
        } finally {
            openSession.close();
        }

    }

}
